package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.CouponHistory;
import com.itheima.mapper.CouponHistoryMapper;
import com.itheima.service.SmsCouponHistoryService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsCouponHistoryServiceImpl implements SmsCouponHistoryService {
    @Autowired
    CouponHistoryMapper smsCouponHistoryMapper;

    //优惠券领取记录
    @Override
    public PageBeanVo list(Integer pageNum, Integer pageSize, Long couponId, String orderSn, Integer useStatus) {
        //设置分页对象
        Page<CouponHistory> page = new Page<>(pageNum, pageSize);

        //创建条件对象
        LambdaQueryWrapper<CouponHistory> lqw = new LambdaQueryWrapper<>();

        //根据优惠券id，使用状态，订单编号分页获取领取记录
        lqw.eq(couponId != null, CouponHistory::getCouponId, couponId);
        lqw.like(StrUtil.isNotEmpty(orderSn), CouponHistory::getOrderSn, orderSn);
        lqw.eq(useStatus != null, CouponHistory::getUseStatus, useStatus);

        Page<CouponHistory> smsCouponHistoryPage = smsCouponHistoryMapper.selectPage(page, lqw);

        //封装PageBeanVo返回
        return new PageBeanVo(pageNum, pageSize, smsCouponHistoryPage.getTotal(), smsCouponHistoryPage.getRecords());
    }
}
